package com.nioserver.propertys;

import com.config.Config;
import com.helper.Debug;
import com.lua.CallLua;
import com.lua.cache.LuaCache;
import com.nioserver.lib.LuaObj;
import com.nioserver.linklayer.Socket;
import com.proto.TMessage;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Role {


    private List<LuaObj> luaObjList;
    public int id;
    public Socket socket;
    public Role(int id,Socket socket)
     {


         this.socket = socket;
         luaObjList =  new ArrayList<>();
        this.id = id;
        File file = new File(Config.LuaPath+"/worker/properties");		//获取其file对象
        File[] fs = file.listFiles();	//遍历path下的文件和目录，放在File数组中
        for(File f:fs){					//遍历File[]数组
            if(!f.isDirectory())		//若非目录(即文件)，则打印
            {
                LuaObj obj =  CallLua.getInstance().launch("worker/properties/"+f.getName().replace(".lua",""));
                obj.serviceName = f.getName().replace(".lua","");
                luaObjList.add(obj);
            }
        }
        file = new File(Config.LuaPath+"/worker/strategies");
        fs = file.listFiles();	//遍历path下的文件和目录，放在File数组中
        for(File f:fs){					//遍历File[]数组
            if(!f.isDirectory())		//若非目录(即文件)，则打印
            {
                LuaObj obj  =  CallLua.getInstance().launch("worker/strategies/"+f.getName().replace(".lua",""));
                obj.serviceName = f.getName().replace(".lua","");
                luaObjList.add(obj);
            }
        }
    }

    public  TMessage.Body serviceInfo(){
        TMessage.Body.Builder body = TMessage.Body.newBuilder();
        for (int i =0;i<luaObjList.size();i++){
            body.addIArr(luaObjList.get(i).address);
            body.addSArr(luaObjList.get(i).serviceName);
        }
        return body.build();
    }

}
